package com.slf.engine.services.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.exception.EngineException;
import com.slf.engine.http.HttpClientManager;
import com.slf.engine.http.HttpResponseWrapper;
import com.slf.engine.services.ILlwwService;
import com.slf.engine.services.helper.ServiceHelper;
import com.slf.engine.utils.Factory;
import com.slf.engine.utils.KeyedDigestMD5;

public class LlwwServiceImpl implements ILlwwService {

	private static final Logger log = Logger.getLogger(LlwwServiceImpl.class);
	
	public Order doPay(Order order)
	{
		ServiceHelper helper = (ServiceHelper)Factory.getInstantBiz("serviceHelper");
		
		String value = "";
		String key = "";
		if("1".equals(SysContants.LLWW_ISQG))
		{
			key = order.getMoney();
			if(!helper.getScore().containsKey(key))
			{
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("没有配置此充值产品");
				return order;
			}
			value = helper.getScore().get(key);
		}else{
			String ssdq[] = order.getSsdq().split("\\s+");
			key = ssdq[0]+order.getMoney();
			if(!helper.getScore().containsKey(key))
			{
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("没有配置此充值产品");
				return order;
			}
			value = helper.getScore().get(key);
		}
		
		String ss[] = value.split("\\|");
		boolean isCheck = true;
		StringBuffer url = new StringBuffer();
//		url.append("gameAccount=" + order.getAccount());
//		url.append("&pID=" + ss[0]);
//		url.append("&userID=" + SysContants.LLWW_LOGINNAME);
//		url.append("&sign=" + KeyedDigestMD5.getKeyedDigest(order.getAccount() + ss[0] + 
//				SysContants.LLWW_LOGINNAME + KeyedDigestMD5.getKeyedDigest(SysContants.LLWW_KEY, ""), ""));
		HttpClientManager manager = new HttpClientManager();
//		try {
//			log.log(Priority.INFO, "发送充值校验:" + url.toString());
//			HttpResponseWrapper wrapper = manager.execute("GET", SysContants.LLWW_CHECK + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
//			String body = new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
//			log.log(Priority.INFO, "充值校验返回:" + body);
//			HtmlCleaner cleaner = new HtmlCleaner();
//			TagNode node = cleaner.clean(body);
//			
//			Object obj[] = node.evaluateXPath("//root/result/text()");
//			if(obj != null && obj.length > 0)
//			{
//				StringBuffer s = (StringBuffer)obj[0];
//				if("1".equals(s.toString()))
//				{
//					isCheck = true;
//				}else{
//					order.setClbz(SysContants.CZ_FAILURE);
//					obj = node.evaluateXPath("//root/errorMsg/text()");
//					order.setCljg(((StringBuffer)obj[0]).toString());
//				}
//			}else{
//				return doPay(order);
//			}
//		} catch (EngineException e) {
//			// TODO Auto-generated catch block
//			return doPay(order);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//		} catch (XPatherException e) {
//			// TODO Auto-generated catch block
//		}
		
		if(isCheck)
		{
			url = new StringBuffer();
			
			url.append("gameAccount=" + order.getAccount());
//			url.append("&logIP="+SysContants.LLWW_IP);
			url.append("&orderNO=" + order.getOrderId());
			url.append("&pid=" + ss[0]);
			url.append("&price="+ss[1]);
			url.append("&proQuantity=1");
			url.append("&userID=" + SysContants.LLWW_LOGINNAME);
			url.append("&sign=" + KeyedDigestMD5.getKeyedDigest(order.getAccount()+SysContants.LLWW_IP+order.getOrderId()+ss[0]+ss[1]+"1"+
			SysContants.LLWW_LOGINNAME+KeyedDigestMD5.getKeyedDigest(SysContants.LLWW_KEY, ""), ""));
			
			try {
				log.log(Priority.INFO, "充值提交:" + url.toString());
				HttpResponseWrapper wrapper = manager.execute("GET", SysContants.LLWW_URL + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
				String body = new String(wrapper.getBody(), SysContants.DEFAULT_CHARSET);
				log.log(Priority.INFO, "充值返回:" + body);
				HtmlCleaner cleaner = new HtmlCleaner();
				TagNode node = cleaner.clean(body);
				Object obj[] = node.evaluateXPath("//root/result/text()");
				if(obj != null && obj.length > 0)
				{
					StringBuffer s = (StringBuffer)obj[0];
					if("1".equals(s.toString()))
					{
						order.setClbz(SysContants.CZ_RUN);
					}else if("-1".equals(s.toString()) || "0".equals(s.toString())){
						order.setClbz(SysContants.CZ_FAILURE);
						obj = node.evaluateXPath("//root/errorMsg/text()"); 
						order.setCljg("充值失败,"+((StringBuffer)obj[0]).toString());
					}else{
						order.setClbz(SysContants.CZ_UNKNOW);
						obj = node.evaluateXPath("//root/error/errorMsg/text()"); 
						order.setCljg(((StringBuffer)obj[0] == null ? "" : obj[0]).toString());
					}
				}
			} catch (EngineException e) {
				// TODO Auto-generated catch block
				order.setClbz(SysContants.CZ_UNKNOW);
				order.setCljg("充值提交出现未知情况，请核实");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
			} catch (IOException e) {
				// TODO Auto-generated catch block
			} catch (XPatherException e) {
				// TODO Auto-generated catch block
			}
			
		}
		
		return order;
	}
	
	
	public Order doQuery(Order order)
	{
		StringBuffer url = new StringBuffer();
		url.append("orderNO=" + order.getOrderId());
		url.append("&userID=" + SysContants.LLWW_LOGINNAME);
		url.append("&sign=" + KeyedDigestMD5.getKeyedDigest(order.getOrderId()+SysContants.LLWW_LOGINNAME+KeyedDigestMD5.getKeyedDigest(SysContants.LLWW_KEY, ""), ""));
		
		log.log(Priority.INFO, "查询提交:" + url.toString());
		HttpClientManager manager = new HttpClientManager();
		try {
			HttpResponseWrapper wrapper = manager.execute("GET", SysContants.LLWW_QUERY + "?" + url.toString(), SysContants.DEFAULT_CHARSET, null, null, null, null, null);
			String body = new String(wrapper.getBody(), SysContants.DEFAULT_CHARSET);
			log.log(Priority.INFO, "查询返回:" + body);
			HtmlCleaner cleaner = new HtmlCleaner();
			TagNode node = cleaner.clean(body);
			Object obj[] = node.evaluateXPath("//root/result/text()");
			if(obj != null && obj.length > 0)
			{
				StringBuffer s = (StringBuffer)obj[0];
				if("2".equals(s.toString()))
				{
					order.setClbz(SysContants.CZ_SUCCESS);
					order.setCljg("充值成功");
				}else if("1".equals(s.toString()) || "0".equals(s.toString())){
					order.setClbz(SysContants.CZ_RUN);
				}else if("3".equals(s.toString())){
					order.setClbz(SysContants.CZ_FAILURE);
					order.setCljg("充值失败");
				}else{
					order.setClbz(SysContants.CZ_UNKNOW);
					obj = node.evaluateXPath("//root/errorMsg/text()");
					order.setCljg(((StringBuffer)obj[0]).toString());
				}
			}
		} catch (EngineException e) {
			// TODO Auto-generated catch block
			return doQuery(order);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
		}
		
		return order;
	}
	
	public static void main(String args[])
	{
		Order order = new Order();
		order.setAccount("13858001723");
		order.setMoney("20");
		order.setOrderId("1123123123");
		new LlwwServiceImpl().doQuery(order);
	}
}
